package com.htt.app.bienbaogiaothong;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.analytics.tracking.android.EasyTracker;
import com.htt.app.bienbaogiaothong.adapter.ListAdapter;
import com.htt.app.bienbaogiaothong.dialog.MyProgressDialog;
import com.htt.app.bienbaogiaothong.menu.Menu;
import com.htt.app.bienbaogiaothong.objects.Details;
import com.htt.app.bienbaogiaothong.sqlite.DAODetails;
import com.htt.app.bienbaogiaothong.utils.Utils;

public class SearchActivity extends Activity implements OnClickListener {
	
	private ImageButton btnShare;
	private ImageButton btnMenu;
	private ImageView icon_search;
	private EditText edt_search;
	private Button btn_search;
	private ImageView no_search;
	
	private ListView listView;
	
	private Activity activity;
	private MyProgressDialog progressDialog;
	
	private List<Details> data = null;
	
	private ListAdapter adapter = null;
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		activity = this;
		
		setContentView(R.layout.search);
		
		btnMenu = (ImageButton) findViewById(R.id.list_btnMenu);
		btnShare = (ImageButton) findViewById(R.id.list_btnShare);
		icon_search = (ImageView) findViewById(R.id.search_icon_search);
		edt_search = (EditText) findViewById(R.id.search_edt_search);
		btn_search = (Button) findViewById(R.id.search_btn_search);
		no_search = (ImageView) findViewById(R.id.search_no_search);
		
		listView = (ListView) findViewById(R.id.list);
		
		loadData();
		
		btnMenu.setOnClickListener(this);
		btnShare.setOnClickListener(this);
		icon_search.setOnClickListener(this);
		btn_search.setOnClickListener(this);
		no_search.setOnClickListener(this);
		
		edt_search.addTextChangedListener(textWatcher);
		
		
	}
	
	// EditText TextWatcher
	private TextWatcher textWatcher = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			// TODO Auto-generated method stub
			if(edt_search.getText().toString().equals("") && no_search.getVisibility() == View.VISIBLE)
					no_search.setVisibility(View.INVISIBLE);
			else if(!edt_search.getText().toString().equals("") && no_search.getVisibility() == View.INVISIBLE)
				no_search.setVisibility(View.VISIBLE);
			
		}
		
		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			
		}
	};
		
	private void loadData() {
		new AsyncTask<Void, Void, Void>() {
			
			protected void onPreExecute() {
				progressDialog = new MyProgressDialog(activity);
				progressDialog.startProgress("", activity.getString(R.string.progress_load));
			};
			
			@Override
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				try {
					DAODetails db = new DAODetails(activity);
					db.createDatabase();
					db.open();
					data = db.getAllDetail();
					db.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			
			protected void onPostExecute(Void result) {
				progressDialog.stopProgress();
				if (Utils.haveNetworkConnection(activity)) {
					try {
						adapter = new ListAdapter(activity, data);
						listView.setAdapter(adapter);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					Utils.showDialog(activity,
							activity.getResources()
									.getString(R.string.error_connect),
									activity.getResources().getString(R.string.connect_info));
				}
			};
			
		}.execute();
		
	}
	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == btnMenu) {
			Menu.rateApp(activity);
		} else if(v == btnShare) {
			Menu.share(activity);
		}  else if(v == no_search) {
			edt_search.setText("");
		} else if (v == icon_search || v == btn_search) {
			InputMethodManager imm = (InputMethodManager)getSystemService(
				      Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(edt_search.getWindowToken(), 0);
			if(!edt_search.getText().toString().trim().equals("")) {
				String text = edt_search.getText().toString().trim()
						.toLowerCase(Locale.getDefault());
				adapter.filter(text);
			}
		}
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}
	
	
}
